package com.ukao.pad.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ukao.pad.R;
import com.ukao.pad.adapter.TagAdapter;
import com.ukao.pad.listener.OnClickPopWindowListener;

import java.util.Set;

public class SpinnerPopWindow extends PopupWindow {
    private Context mContext;
    private TagFlowLayout mListView;
    private TagAdapter mAdapter;
    private OnClickPopWindowListener mItemSelectListener;
    private TextView mTextView;
    private String mMessage;
    private int selectPos;
    private int defaultPos;

    public SpinnerPopWindow(Context context) {
        super(context);
        mContext = context;
        init();
    }
    public SpinnerPopWindow(Context context, String message) {
        super(context);
        mContext = context;
        mMessage = message;
        init();
    }


    public void setItemListener(OnClickPopWindowListener listener) {
        mItemSelectListener = listener;
    }

    public void setAdatper(TagAdapter adapter, int defaultpos) {
        mAdapter = adapter;
        mListView.setAdapter(mAdapter);
        mAdapter.setSelectedList(defaultpos);
        mListView.setOnSelectListener(mOnSelectListener);
    }


    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popwindow_select_stores,null);
        setContentView(view);
        setWidth(LayoutParams.WRAP_CONTENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setTouchable(true);
        setFocusable(false);
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#00000000"));
        setBackgroundDrawable(dw);
        setOutsideTouchable(false);
        mListView = (TagFlowLayout) view.findViewById(R.id.tag_flow_layout);
        StateImageView closeWindow= (StateImageView)view.findViewById(R.id.img_close_btn);
        Button submitBtn=(Button)view.findViewById(R.id.btn_submit);
        Button cancelBtn=(Button)view.findViewById(R.id.sb_cancel);
        cancelBtn.setOnClickListener(mOnClickListener);
        submitBtn.setOnClickListener(mOnClickListener);
        closeWindow.setOnClickListener(mOnClickListener);
    }
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.img_close_btn:
                    dismiss();
                    break;
                case R.id.btn_submit:
                    mItemSelectListener.onSureItemClick(mAdapter.getItem(selectPos));
                    break;
                case R.id.sb_cancel:
                    dismiss();
                    break;
            }
        }
    };
    private TagFlowLayout.OnSelectListener mOnSelectListener = new TagFlowLayout.OnSelectListener() {
        @Override
        public void onSelected(Set<Integer> selectPosSet) {
            for (Integer pos :
                    selectPosSet) {
                selectPos = pos;
            }
            Log.d("触发了", "onSelected: ");
//            if (mItemSelectListener != null) {
//                Object mitem = mAdapter.getItem(selectPos);
//                mItemSelectListener.onSureItemClick(mitem);
//            }
        }
    };


}
